package com.quarters.util.uploadFile;

import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Zhang on 2018/10/8.
 */
public class FileUpload {





    public  static String upload(MultipartFile file ) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
        String res = sdf.format(new Date());
        String fileName = file.getOriginalFilename();
        int size = (int) file.getSize();
        System.out.println("文件大小:" +  size);

        // uploads文件夹位置
        File path   = new File(ResourceUtils.getURL("classpath:").getPath());
        System.out.println("文件位置:"+path);
        String a = path.getParentFile().getParentFile()+File.separator+"fileUploads" ;
        String rootPath = getJarRootPath()+"/uploads/" ;
        System.out.println("路径:"+rootPath);
        //String rootPath = request.getSession().getServletContext().getRealPath("resource/FileUploads/");
        // 原始名称
        String originalFileName = file.getOriginalFilename();
        System.out.println("原始文件:"+originalFileName);
        // 新文件名
        String newFileName = "sliver"  +res+ originalFileName.substring(originalFileName.lastIndexOf("."));
        System.out.println("新文件名:"+newFileName);
        // 创建年月文件夹
        Calendar date = Calendar.getInstance();
        File dateDirs = new File(date.get(Calendar.YEAR) + File.separator + (date.get(Calendar.MONTH)+1));

        // 新文件
        File newFile = new File(rootPath + File.separator + dateDirs + File.separator + newFileName);
        // 判断目标文件所在目录是否存在
        if( !newFile.getParentFile().exists()) {
            // 如果目标文件所在的目录不存在，则创建父目录
            newFile.getParentFile().mkdirs();
        }
        System.out.println("newFile:"+newFile);
        //String b = newFile+"";
        // 将内存中的数据写入磁盘
        file.transferTo(newFile);

        // 完整的url
        String fileUrl = date.get(Calendar.YEAR) + "/" + (date.get(Calendar.MONTH)+1) + "/" + fileName;
        System.err.println("fileUrls:"+  newFile.getPath().replaceAll("\\\\", "/"));
        return  newFile.getPath().replaceAll("\\\\", "/");
    }



    private static String getJarRootPath() throws FileNotFoundException {
        String path = ResourceUtils.getURL("classpath:").getPath();
        //=> file:/root/tmp/demo-springboot-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/
        //System.out.println("ResourceUtils.getURL(\"classpath:\").getPath() -> "+path);
        //创建File时会自动处理前缀和jar包路径问题 => /root/tmp
        File rootFile = new File(path);
        if(!rootFile.exists()) {
            System.out.println("根目录不存在, 重新创建");
            rootFile = new File("");
            System.out.println("重新创建的根目录: "+rootFile.getAbsolutePath()); }
        System.out.println("项目根目录: "+rootFile.getAbsolutePath());
        //获取的字符串末尾没有分隔符 /
        return rootFile.getAbsolutePath();
    }
}
